package com.zzuli.fitness.controller;

import com.zzuli.fitness.entity.User;
import com.zzuli.fitness.service.UserService;
import com.zzuli.fitness.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "会员接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "微信登录")
    @GetMapping("/login")
    public Result login(@RequestParam("openid") String openid,
                        @RequestParam("nickName") String nickName,
                        @RequestParam("sex") String sex){
        User user = userService.selectUserByOpenId(openid);
        if (user==null){
            User aUser = new User();
            aUser.setId(null);
            aUser.setOpenid(openid);
            aUser.setNickname(nickName);
            aUser.setSex(sex);
            userService.addNewUser(aUser);
            return Result.ok(aUser);
        }
        return Result.ok(user);
    }

    @ApiOperation(value = "完善个人信息")
    @GetMapping("/userInfo")
    public Result userInfo(@RequestBody User user){
        userService.updateUserInfo(user);
        return Result.ok();
    }
}
